//
//  GuiderDetailVC.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/12/27.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "GuiderDetailVC.h"
#import "GuiderDetailLayout.h"
#import "GuiderSectionHeadView.h"

#import "GuiderDetialHeadCell.h"
#import "LineNodeCCell.h"
#import "LineNodeSmallCCell.h"
#import "LinePreDetailVC.h"
#import "SpecialDetailVC.h"
#import "IGAvPlayerManager.h"
@interface GuiderDetailVC () <UICollectionViewDelegate, UICollectionViewDataSource, GuiderDetailLayoutDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) GuiderModel *guider;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewTop;
@property (weak, nonatomic) IBOutlet GuiderDetailLayout *layout;

@property (nonatomic) NSInteger type;// 0大图；1 小图

@property (nonatomic) BOOL isLine;// 线路/专题

@end

@implementation GuiderDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人主页";
    [self.collectionView registerNib:[UINib nibWithNibName:@"LineNodeCCell" bundle:nil] forCellWithReuseIdentifier:@"LineNodeCCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"LineNodeSmallCCell" bundle:nil] forCellWithReuseIdentifier:@"LineNodeSmallCCell"];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"GuiderDetialHeadCell" bundle:nil] forCellWithReuseIdentifier:@"GuiderDetialHeadCell"];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"GuiderSectionHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"GuiderSectionHeadView"];
    self.layout.delegate = self;
    self.layout.fixTop = 0;
    [self closeAutomaticallyAdjustsScrollViewInsets:self.collectionView];
    self.collectionViewTop.constant = kNavigationBarHeight;
//    self.layout.isStickyHeader = YES;
//    self.layout.isTopForHeader = YES;
    
    self.isLine = YES;
    self.type = 0;
    
    
    [self setupData];
    
    [self configEmptyDataTableView:self.collectionView emptyImageName:@"暂无收藏" describe:MYLocalizedString(@"PUBLIC_NODATA_GUIDER", nil) refresh:nil block:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    if ([IGAvPlayerManager shareManeger].isPlay && [[IGAvPlayerManager shareManeger].currentURL isEqualToString:self.guider.audiointroduction]) {
        [[IGAvPlayerManager shareManeger] stop];
    }
}

- (void)setupData {
    
    [ActivityIndicatorView showHUDAddedTo:self.view backgroundColor:[UIColor whiteColor]];
    
    [NetAPIManager getGuiderDetailWith:self.guiderID cityid:nil block:^(GuiderModel *guider, NSError *error) {
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        self.guider = guider;
        self.title = guider.realname;
        self.dataHasLoad = YES;
        if (guider.lines.count == 0) {
            self.isLine = NO;
        }
        
        [self.collectionView reloadData];
        
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    if (!self.guider) {
        
        return 0;
    }
    
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }
    
    return self.isLine ? self.guider.lines.count : self.guider.albums.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        GuiderDetialHeadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GuiderDetialHeadCell" forIndexPath:indexPath];
        cell.guider = self.guider;
        return cell;
    }
    
    LineModel *model = self.isLine ? self.guider.lines[indexPath.row] : self.guider.albums[indexPath.row];
    if (self.type == 1) {
        
        LineNodeSmallCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LineNodeSmallCCell" forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
    
    
    LineNodeCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LineNodeCCell" forIndexPath:indexPath];
    cell.model = model;
    cell.type = 1;
    return cell;
}

#pragma mark - 头部或者尾部视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        GuiderSectionHeadView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"GuiderSectionHeadView" forIndexPath:indexPath];
        header.delegate = self;
        header.isSelectLine = self.isLine;
        
        NSMutableArray *typeArray = [NSMutableArray array];
        
        if (self.guider.lines.count > 0) {
            
            [typeArray addObject:@"line"];
        }
        if (self.guider.albums.count > 0) {
            
            [typeArray addObject:@"special"];
        }
        
        header.typeArray = typeArray;
        
        return header;
    }
    
    return nil;
}
// 0, 1 :大图/小图；2, 3 : 线路/专题。
- (void)changeTypeWithType:(NSInteger)type  {
    
    if (self.type == type) {
        return;
    }
    

    if (type <= 1) {
        self.type = type;
    } else {
        
        BOOL isline = (type == 2 ? YES : NO);
        
        if (isline == self.isLine) {
            return;
        }
        self.isLine = isline;
    }
    [self.collectionView scrollToTopAnimated:NO];
    
    [self.collectionView reloadData];
}

- (CGFloat)collectionView:(nonnull UICollectionView *)collectionView
                   layout:(nonnull GuiderDetailLayout *)collectionViewLayout
    widthForItemInSection:( NSInteger )sectio {
    
    if (sectio == 0) {
        return kScreenWidth;
    }
    if (self.type == 1) {
        return (kScreenWidth - 32 - 32) / 3;
    }
    
    return kScreenWidth - 32;
}
- (CGFloat)collectionView:(nonnull UICollectionView *)collectionView
                   layout:(nonnull GuiderDetailLayout *)collectionViewLayout
 heightForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return [GuiderDetialHeadCell getCellHeightWithContentText:self.guider.introduction];
    }
    
    if (self.type == 1) {
        return (kScreenWidth - 32 - 32) / 3 + 35;
    }
    
    
    CGFloat width = kScreenWidth - 32;
    CGFloat height = width * 9 / 16 + 72;
    
    return height;
}

- (CGFloat)collectionView:(nonnull UICollectionView *)collectionView
                    layout:(nonnull GuiderDetailLayout *)collectionViewLayout
heightForHeaderAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 0;
    }
    
    return 50;
}

- (CGFloat)collectionView:(nonnull UICollectionView *)collectionView
                    layout:(nonnull GuiderDetailLayout *)collectionViewLayout
              topInSection:(NSInteger )section {
    
    if (section == 0) {
        return 0;
    }
    
    return 8;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return;
    }
    
    LineModel *model = self.isLine ? self.guider.lines[indexPath.row] : self.guider.albums[indexPath.row];
    
    if (model.type.integerValue == 4) {
        
        SpecialDetailVC *specialVC = [SpecialDetailVC new];
        specialVC.specialid = model.lineid;
        [self.navigationController pushViewController:specialVC animated:YES];
        
        return;
    }
    
    LinePreDetailVC *detailVC = [IGUtil loadViewController:@"LinePreDetailVC" storyboard:@"LineDetail"];
    detailVC.lineid = model.lineid;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}


@end
